package com.lion.appletrip.fragment;


import com.lion.appletrip.LoginActivity;
import com.lion.appletrip.R;
import com.romainpiel.titanic.library.Titanic;
import com.romainpiel.titanic.library.TitanicTextView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * A simple {@link Fragment} subclass. Activities that contain this fragment
 * must implement the {@link Fragment4.OnFragmentInteractionListener} interface
 * to handle interaction events. Use the {@link Fragment4#newInstance} factory
 * method to create an instance of this fragment.
 *
 */
@SuppressLint("SetJavaScriptEnabled")
public class Fragment4 extends Fragment
{

	private WebView mWebView;
	private TitanicTextView ivLoading;
	private Titanic titanic;

	public Fragment4()
	{
		// Required empty public constructor
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState)
	{
		// Inflate the layout for this fragment
		View layout = inflater.inflate(R.layout.fragment_fragment4, container,
		        false);
		mWebView = (WebView) layout.findViewById(R.id.webView1);
		ivLoading = (TitanicTextView) layout.findViewById(R.id.loading_fg4);
		titanic = new Titanic();
		titanic.start(ivLoading );
		WebSettings settings = mWebView.getSettings();
		settings.setDomStorageEnabled(true);
		settings.setJavaScriptEnabled(true);
		settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		mWebView.loadUrl("http://web.breadtrip.com/product/topics/");
		mWebView.setOnKeyListener(new OnKeyListener()
		{

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack())
				{
					mWebView.goBack();
					return true;
				}
				return false;
			}
		});
		// mWebView.setWebViewClient(new WebViewClient());
		mWebView.setWebViewClient(new WebViewClient()
		{

			@Override
			public void onReceivedError(WebView view, int errorCode,
			        String description, String failingUrl)
			{
				// super.onReceivedError(view, errorCode, description,
				// failingUrl);

			}

			public boolean shouldOverrideUrlLoading(WebView view, String url)
			{
				if (url != null && url.startsWith("tel:"))
				{
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
					        .parse(url));
					startActivity(intent);
					return true;
				}
				view.loadUrl(url);

				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url)
			{
				titanic.cancel();
				ivLoading.setVisibility(View.GONE);
				mWebView.setVisibility(View.VISIBLE);
				if (url != null && url.contains("order"))
				{
					Intent intent = new Intent(getContext(),
					        LoginActivity.class);
					startActivity(intent);
				
				}
				super.onPageFinished(view, url);
			}

			@Override
			public void onReceivedError(WebView view,
			        WebResourceRequest request, WebResourceError error)
			{
				view.loadDataWithBaseURL(null, "加载失败...", "text/html", "UTF-8",null);
				titanic.cancel();
				ivLoading.setVisibility(View.GONE);
				super.onReceivedError(view, request, error);
			}
		});

		return layout;

	}

}
